package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

@Configurable
@Component
@RooDataOnDemand(entity = Ttlsbont.class)
public class TtlsbontDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<Ttlsbont> data;

	public Ttlsbont getNewTransientTtlsbont(int index) {
        Ttlsbont obj = new Ttlsbont();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setTimTimestam(obj, index);
        return obj;
    }

	public void setEmbeddedIdClass(Ttlsbont obj, int index) {
		TtlsbontPK embeddedIdClass;
		if (String.valueOf(index).length() > 1){
			embeddedIdClass = new TtlsbontPK("codLogonapl_" + String.valueOf(index).substring(0, 7), "codUsu_" + String.valueOf(index).substring(0, 1), "c_" + String.valueOf(index).substring(0, 1));
		}else {
			embeddedIdClass = new TtlsbontPK("codLogonapl_" + index, "codUsu_" + index, "c_" + index);
		}
        obj.setId(embeddedIdClass);
    }

	public void setAudFmodific(Ttlsbont obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	public void setAudUsuario(Ttlsbont obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	public void setTimTimestam(Ttlsbont obj, int index) {
        Date timTimestam = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setTimTimestam(timTimestam);
    }

	public Ttlsbont getSpecificTtlsbont(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbont obj = data.get(index);
        return Ttlsbont.findTtlsbont(obj.getId());
    }

	public Ttlsbont getRandomTtlsbont() {
        init();
        Ttlsbont obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbont.findTtlsbont(obj.getId());
    }

	public boolean modifyTtlsbont(Ttlsbont obj) {
        return false;
    }

	public void init() {
        data = Ttlsbont.findTtlsbontEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbont' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbont>();
        for (int i = 0; i < 10; i++) {
            Ttlsbont obj = getNewTransientTtlsbont(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
